<template>
    <div id="container"></div>
</template>

<script setup>
import { onBeforeMount, onMounted, onUnmounted } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";

let props = defineProps(["transmitBuilding3"])
let map = null;
let marker = null;

onBeforeMount(() => {
    window._AMapSecurityConfig = {
        securityJsCode: "2d76e7d7684c3596224871cadd2b3c9b",
    };
    AMapLoader.load({
        key: "186ca59d7756ca9423e2c2554d0cb916", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ["AMap.Scale"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
    })
        .then((AMap) => {
            map = new AMap.Map("container", {
                // 设置地图容器id
                viewMode: "3D", // 是否为3D地图模式
                zoom: 17, // 初始化地图级别
                center: [106.595224, 29.42207], // 初始化地图中心点位置
            });
            if (props.transmitBuilding3.longitude != 0 && props.transmitBuilding3.latitude != 0) {
                marker = new AMap.Marker({
                    position: new AMap.LngLat(props.transmitBuilding3.longitude, props.transmitBuilding3.latitude),
                    offset: new AMap.Pixel(-13, -30),
                    icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png", //添加 icon 图标 URL
                    //  title: props.transmitBuilding3.name,
                });
                map.add(marker);
            }
            //获取点击位置的信息
            map.on('click', function (e) {
                if (marker) {
                    marker.setMap(null);
                    marker = null;
                }
                props.transmitBuilding3.longitude=e.lnglat.getLng()
                props.transmitBuilding3.latitude=e.lnglat.getLat()
                marker = new AMap.Marker({
                    position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()),
                    offset: new AMap.Pixel(-13, -30),
                    icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png", //添加 icon 图标 URL
                    title: e.name,
                });
                map.add(marker);

            })
        })
        .catch((e) => {
            console.log(e);
        });
});

onUnmounted(() => {
    map?.destroy();
});
</script>

<style scoped>
#container {
    width: 100%;
    height: 700px;
}
</style>